package com.lezhu.plcsdklibrary;

import android.util.Log;

import com.lezhu.plcsdklibrary.manager.DoorStatusCallBackManager;
import com.lezhu.plcsdklibrary.manager.InfraredSensorCallBackManager;
import com.lezhu.plcsdklibrary.manager.OpenDoorCallBackManager;

/**
 * 描述：命令处理工具
 * 作者: 钮家齐
 * 时间: 2020/5/25 16:51
 */
public class CmdUtil {
    private static final String TAG = "CmdUtil";

    /**
     * 处理下位机返回的命令
     */
    public static void processResultCmd(String cmd) {

        OperatListUtls.getInstance().addLog("接收到下位机发送的指令:"+cmd,2);
        //硬件地址   0B plc   0A android机
        String hardware_address = cmd.substring(0, 2);
        //功能吗
        String gongneng = cmd.substring(2, 4);

        Log.i(TAG, "硬件地址:" + hardware_address +
                "       功能码:" + gongneng);

        if (hardware_address.equals("0B")) {
            //todo 因为是下位机主动通知主机，所以主机要返回同样的指令给下位机，表示确认收到
            PlcSDK.getInstance().ReplyCmd(cmd);
//            mainActivity.ReplyCmd(cmd);
            String door = "";
            String status = "";
            if (gongneng.equals("02") || gongneng.equals("03") || gongneng.equals("04")) {
                //柜门位置
                door = cmd.substring(8, 10);
                //柜门状态
                status = cmd.substring(10, 12);
            }
            switch (gongneng) {
                case "02":
                    //todo 开门指令  看05功能码这个不看
                    if (status.equals("00")) {
                        int tempdoor = Integer.parseInt(door,16);
                        tempdoor += 1;
                        //正常打开
                        Log.e(TAG, tempdoor + "号柜门可以正常打开");
//                        Toast.makeText(mainActivity, door + "号柜门正常打开", Toast.LENGTH_SHORT).show();
                    }
                    if (status.equals("10")) {
                        //已经打开
                        Log.e(TAG, door + "号柜门已经打开");
//                        Toast.makeText(mainActivity, door + "号柜门已经打开", Toast.LENGTH_SHORT).show();
                    }
                    if (status.equals("11")) {
                        //电流过载
                        Log.e(TAG, door + "号柜门电流过载");
//                        Toast.makeText(mainActivity, door + "号柜门电流过载", Toast.LENGTH_SHORT).show();
                    }
                    if (status.equals("12")) {
                        //通信异常
                        Log.e(TAG, door + "号柜门通信异常");
//                        Toast.makeText(mainActivity, door + "号柜门通信异常", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case "03":
                case "04":
                    //todo 查询整机柜门状态
                    //todo 查询单个柜门状态
                    if (status.equals("00")) {
                        Log.e(TAG, door + "号柜门打开状态");
                    } else {
                        Log.e(TAG, door + "号柜门关闭状态");
                    }
                    break;
                case "06":
                    //todo 红外感应  人体靠近
                    String ganying = cmd.substring(8, 10);
                    if (ganying.equals("01")) {
                        Log.e(TAG, "检测到人体靠近");
                        InfraredSensorCallBackManager.getInstance().come();
                        OperatListUtls.getInstance().addLog("人体靠近");
                    }
                    break;
                case "05":
                    //todo 柜门关闭  主动通知
                    String pos = cmd.substring(8, 10);

                    int myposi = Integer.parseInt(pos,16);
                    myposi += 1;
                    String status9 = cmd.substring(10, 12);
                    String msg = myposi + "号柜门已" + (status9.equals("00") ? "打开" : "关闭");
                    OperatListUtls.getInstance().addLog(msg);

                    if (status9.equals("00")) {
                        DoorStatusCallBackManager.getInstance().doorOpen(myposi);
                        OpenDoorCallBackManager.getInstance().onSuccess(myposi);
                    } else {
                        DoorStatusCallBackManager.getInstance().doorClose(myposi);
                    }
                    break;
                case "42":
                    //todo 主板重启
                    String status2 = cmd.substring(8, 10);
                    if (status2.equals("00")) {
                        Log.e(TAG, "重启正常");
                    } else {
                        Log.e(TAG, "重启异常");
                    }
                    break;
            }
        } else {
            switch (gongneng) {
                case "02":
                    Log.e(TAG, "下位机确认收到开门指令");
                    break;
                case "03":
                    Log.e(TAG, "下位机确认收到查询单个柜门");
                    break;
                case "04":
                    Log.e(TAG, "下位机确认收到查询整机柜门");
                    break;
                case "42":
                    Log.e(TAG, "下位机确认收到主板重启指令");
                    break;
            }
        }

    }
}
